package com.twotiger.and.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;
import app.twotiger.p2p.R;

import com.twotiger.and.Constants;
import com.twotiger.and.TwoTigerApp;
import com.twotiger.and.activity.HomeActivity;
import com.twotiger.and.activity.project.ProjectDetailPage;
import com.twotiger.and.activity.project.Projectpage;
import com.twotiger.and.activity.project.debt.ProjectDebtDetailPage;
import com.twotiger.and.activity.user.LoginPage;
import com.twotiger.and.bean.DebtTransProjectDetail;
import com.twotiger.and.bean.ProjectDetail;
import com.twotiger.and.util.ArithUtils;
import com.twotiger.and.util.ListUtils;
import com.twotiger.and.util.PromptManager;
import com.twotiger.and.util.PromptManager.ReChargeClickListener;
import com.twotiger.and.view.CircleProgress;

/**
 * Created by victor on 2014/11/7 0007.
 */
public class ProjectDebtAdapter extends BaseAdapter {
    private HomeActivity mactivity;
    private ArrayList<DebtTransProjectDetail> mList;
    private Projectpage mFragment;

    public ProjectDebtAdapter(HomeActivity _Activity, ArrayList<DebtTransProjectDetail> _List,
                              Projectpage fragment) {
        this.mactivity = _Activity;
        this.mList = _List;
        this.mFragment = fragment;
    }

    public void addData(List<DebtTransProjectDetail> _projectItems) {
        if (!ListUtils.isEmpty(_projectItems)) {
            mList.addAll(_projectItems);
        }
        notifyDataSetChanged();
    }

    public void resetData(List<DebtTransProjectDetail> _List) {
        mList.clear();
        if (!ListUtils.isEmpty(_List)) {
            mList.addAll(_List);
        }
        notifyDataSetChanged();
    }

    public ArrayList<DebtTransProjectDetail> getDataList() {
        return mList;
    }

    @Override
    public int getCount() {
        if (!ListUtils.isEmpty(mList)) {
            return mList.size();
        }else {
            return 10;
        }
    }

    @Override
    public DebtTransProjectDetail getItem(int position) {
        if (!ListUtils.isEmpty(mList) && position < mList.size()) {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * @param position
     * @param convertView
     * @param parent
     * @return
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = View.inflate(mactivity, R.layout.project_tiger_debt_item, null);
        }
        TextView project_name_tv = ViewHolder.get(convertView, R.id.project_name_tv);
        TextView tv_discount_tag = ViewHolder.get(convertView, R.id.tv_discount_tag);
        TextView project_amount_tv = ViewHolder.get(convertView, R.id.project_amount_tv);
        TextView project_period_tv = ViewHolder.get(convertView, R.id.project_period_tv);
        TextView project_yield_tv = ViewHolder.get(convertView, R.id.project_yield_tv);
        Button btn_buy = ViewHolder.get(convertView, R.id.btn_buy);
        TextView tv_mortgage_type = ViewHolder.get(convertView, R.id.tv_mortgage_type);
           
        final DebtTransProjectDetail projectItemofDebt = getItem(position);
        if (projectItemofDebt != null) {
            tv_mortgage_type.setText(projectItemofDebt.getPledgeType());
            project_name_tv.setText(projectItemofDebt.getName() + "");
            project_yield_tv.setText(ArithUtils.formatProject(projectItemofDebt.getYield(), "#.#") + "");
            project_period_tv.setText(projectItemofDebt.getRemainDays()+"");
            project_amount_tv.setText(ArithUtils.coverMoneyComma(""+projectItemofDebt.getRemainAmount()));
            if(projectItemofDebt.getDiscountMark() == 0){
                tv_discount_tag.setVisibility(View.GONE);
            }else if(projectItemofDebt.getDiscountMark() == 1){
                tv_discount_tag.setVisibility(View.VISIBLE);
                tv_discount_tag.setText(ArithUtils.coverMoney(projectItemofDebt.getAssignRate()/10+"")+" 折");
            }

            //1-抢；2-还款中；3-已完成
            switch (projectItemofDebt.getStatus()) {
            case 1: // 抢
                btn_buy.setText("立即抢购");
                btn_buy.setEnabled(true);
                
                double progress = ArithUtils.div(projectItemofDebt.getCompleteLv(), 100, 4); 
                int progress_int = (int) Math.round(progress * 10000);
                break;
            case 2: // 还款中
                btn_buy.setText(projectItemofDebt.getStatusName() + "");
                btn_buy.setEnabled(false);
                
                break;
            case 3: // 已完成
                btn_buy.setText(projectItemofDebt.getStatusName() + "");
                btn_buy.setEnabled(false);
                break;
            }
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mactivity, ProjectDebtDetailPage.class).putExtra(
                        "DEBT_TRANS_MARK", projectItemofDebt.getbId());
                    mactivity
                    .startNewActivity(intent, R.anim.push_left_in, R.anim.push_left_out, false);
                }
            });
            btn_buy.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (projectItemofDebt.getStatus() != 1) {
                        return;
                    }
                    if (!isLogin()) {
                        Intent intent = new Intent(mactivity, LoginPage.class).putExtra("FROM",
                            R.id.to_inverst);
                        mactivity.startNewActivity(intent, R.anim.slide_in_from_bottom,
                            R.anim.silent_anim, false);
                        return;
                    }
                    if (!hasOpenThirdAct()) {
                        PromptManager.showConfirmAlertCommon(mactivity, 0, "",
                            "您还未开通汇付天下托管账号，继续开通", "取消", "继续", true, new ReChargeClickListener() {

                                @Override
                                public void onClickConfirm() {

                                }

                                @Override
                                public void onClickCancel() {
                                    // 确认，则继续
                                    mFragment.openpayment();
                                }
                            });
                        return;
                    }
                    mFragment.getProjectDetail(projectItemofDebt.getbId());
                }

            });
        }

        return convertView;
    }
    
    protected boolean hasOpenThirdAct() {
        return TwoTigerApp.getInstance().getUser().hasOpenThirdAct;
    }
    
    protected boolean isLogin() {
        if (!TextUtils.isEmpty(TwoTigerApp.getInstance().getUser().token)) {
            return true;
        }
        return false;
    }

}
